package com.enthusiasm.constants;

import java.io.IOException;

import com.young.R;

import android.content.Context;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.media.MediaPlayer.OnPreparedListener;

public class SoundManager {
	private static SoundManager instance;
	private static Object INSTANCE_LOCK = new Object();
	private MediaPlayer mediaPlayer;
	private SoundPool soundPool;
	private Context context;
	private boolean isMediaStop = false;
	private boolean isSoundStop = false;
	private int clickSoundID;
	
	public SoundManager(Context context) {
		this.context = context;
		initBackgroudSound();
		initSoundPool();
		loadManagerCfg();
	}
	
	private void loadManagerCfg() {
		SharedPreferences sp = context.getSharedPreferences("managercfg", Context.MODE_PRIVATE);
		boolean isMediaStop = sp.getBoolean("isMediaStop", false);
		boolean isSoundStop = sp.getBoolean("isSoundStop", false);
		this.isMediaStop = isMediaStop;
		this.isSoundStop = isSoundStop;
	}

	public void initSoundPool() {
		if(soundPool == null){
			soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
			clickSoundID = soundPool.load(context, R.drawable.click, 0);
		}
	}

	public boolean getIsMediaStop(){
		return isMediaStop;
	}
	
	public void setIsMediaStop(boolean isMediaStop){
		this.isMediaStop = isMediaStop;
		SharedPreferences sp = context.getSharedPreferences("managercfg", Context.MODE_PRIVATE);
		sp.edit().putBoolean("isMediaStop", isMediaStop).commit();
	}
	
	public boolean getIsSoundStop(){
		return isSoundStop;
	}
	
	public void setIsSoundStop(boolean isSoundStop){
		this.isSoundStop = isSoundStop;
		SharedPreferences sp = context.getSharedPreferences("managercfg", Context.MODE_PRIVATE);
		sp.edit().putBoolean("isSoundStop", isSoundStop).commit();
	}
	
	public static SoundManager getInstance(Context context){
		synchronized (INSTANCE_LOCK) {
			if(instance == null){
				instance = new SoundManager(context);
			}
		}
		return instance;
	}
	
	private void initBackgroudSound() {
		if(mediaPlayer == null)
			mediaPlayer = MediaPlayer.create(context,R.drawable.background_mediaplayer);
		try {
			mediaPlayer.prepare();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
				public void onPrepared(MediaPlayer mp) {
					mediaPlayer.setLooping(true);
				}
			});
		}
	}
	
	public void pauseBackgroundSound(){
		if(mediaPlayer.isPlaying())
			mediaPlayer.pause();	
	}
	
	public void resumeBackGroundSound(){
		if(!mediaPlayer.isPlaying())
			mediaPlayer.start();
	}
	
	public void pauseSoundPool(){
	}
	
	public void resumeSoundPool(){
		
	}
	
	public void playSoundClick(){
		if(!isSoundStop){
			if(soundPool != null)
				soundPool.play(clickSoundID , 1, 1, 0, 0, 1);
		}
	}
	
	public void releaseSoundPool(){
		if(soundPool != null){
			soundPool.release();
			soundPool = null;
		}
	}
}
